const express = require('express')
const md5 = require('md5')
const router = express.Router()
const db = require('../db/index')
//导入token
// const jwt=require('jsonwebtoken')
// const config=require('../config')
// 进行token编码
const Token = require('../router_handler/token')
const verify = require('../router_handler/verify')
//信息提示
const Constant = require('../router/error/constant');

//获取客户列表
router.get('/getclient', verify.verifyToken, (req, res) => {
    let sqlstr = 'select * from client where is_del=0'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        //保存查询数据
        let data = results
        res.send({
            data,
            ...Constant.DATA_SUCCESS
        })
    })
})
//添加用户
router.post('/addclient', verify.verifyToken, (req, res) => {
    let data = req.body
    // console.log(data);
    let sqlstr = 'INSERT INTO client (client_name,client_address,client_person,client_phone,is_del,lat,lng) values("' + data.client_name + '","' + data.client_address + '","' + data.client_person + '","' + data.client_phone + '","' + data.is_del + '","' + data.lat + '","' + data.lng + '")'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '添加失败,请稍后再试'
            })
        }
        res.send({
            status: 201,
            message: '添加成功'
        })

    })
})
//修改用户
router.post('/editclient', verify.verifyToken, (req, res) => {
    let data = req.body
    // if (data.powerid==undefined) {
    //     return res.send({
    //         status: 403,
    //         message: '修改失败,必须含有一个权限'
    //     })
    // }
    let sqlstr = 'UPDATE client SET client_name="' + data.client_name + '",client_address="' + data.client_address + '",client_person="' + data.client_person + '",client_phone="' + data.client_phone + '",lat="' + data.lat + '",lng="' + data.lng + '"where cid="' + data.cid + '"'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '修改失败,请稍后再试'
            })
        }
        res.send({
            status: 201,
            message: '修改成功'
        })

    })
})
//删除用户
router.post('/delclient', verify.verifyToken, (req, res) => {
    let data = req.body
    let sqlstr = 'UPDATE client SET is_del="' + 1 + '"where cid="' + data.cid + '"'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '删除失败,请稍后再试'
            })
        }
        res.send({
            status: 201,
            message: '删除成功'
        })

    })
})
//搜索客户列表
router.post('/searchclient', verify.verifyToken, (req, res) => {
    let data = req.body
    console.log(data);
    let sqlstr = ''
    if (data.left != '' && data.right != '') {
        sqlstr = 'select * from client where client_name LIKE "%' + data.left + '%" and client_person LIKE "%' + data.right + '%" and is_del=0'
    } else if (data.left != '') {
        sqlstr = 'select * from client where client_name LIKE "%' + data.left + '%" and is_del=0'
    } else {
        sqlstr = 'select * from client where client_person LIKE "%' + data.right + '%" and is_del=0'
    }
    console.log(sqlstr);
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        //保存查询数据
        let data = results
        res.send({
            data,
            ...Constant.DATA_SUCCESS
        })
    })
})

//获取供应商列表
router.get('/getsupply', verify.verifyToken, (req, res) => {
    let sqlstr = 'select * from supply where is_del=0'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        //保存查询数据
        let data = results
        res.send({
            data,
            ...Constant.DATA_SUCCESS
        })
    })
})
//添加供应商
router.post('/addsupply', verify.verifyToken, (req, res) => {
    let data = req.body
    // console.log(data);
    let sqlstr = 'INSERT INTO supply (supply_name,supply_address,supply_person,supply_phone,is_del,lat,lng) values("' + data.supply_name + '","' + data.supply_address + '","' + data.supply_person + '","' + data.supply_phone + '","' + data.is_del + '","' + data.lat + '","' + data.lng + '")'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '添加失败,请稍后再试'
            })
        }
        res.send({
            status: 201,
            message: '添加成功'
        })

    })
})
//修改用户
router.post('/editsupply', verify.verifyToken, (req, res) => {
    let data = req.body
    // if (data.powerid==undefined) {
    //     return res.send({
    //         status: 403,
    //         message: '修改失败,必须含有一个权限'
    //     })
    // }
    let sqlstr = 'UPDATE supply SET supply_name="' + data.supply_name + '",supply_address="' + data.supply_address + '",supply_person="' + data.supply_person + '",supply_phone="' + data.supply_phone + '",lat="' + data.lat + '",lng="' + data.lng + '"where sid="' + data.sid + '"'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '修改失败,请稍后再试'
            })
        }
        res.send({
            status: 201,
            message: '修改成功'
        })

    })
})
//删除供应商
router.post('/delsupply', verify.verifyToken, (req, res) => {
    let data = req.body
    let sqlstr = 'UPDATE supply SET is_del="' + 1 + '"where sid="' + data.sid + '"'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '删除失败,请稍后再试'
            })
        }
        res.send({
            status: 201,
            message: '删除成功'
        })

    })
})
//搜索供应商列表
router.post('/searchsupply', verify.verifyToken, (req, res) => {
    let data = req.body
    // console.log(data);
    let sqlstr = ''
    if (data.left != '' && data.right != '') {
        sqlstr = 'select * from supply where supply_name LIKE "%' + data.left + '%" and supply_person LIKE "%' + data.right + '%" and is_del=0'
    } else if (data.left != '') {
        sqlstr = 'select * from supply where supply_name LIKE "%' + data.left + '%" and is_del=0'
    } else {
        sqlstr = 'select * from supply where supply_person LIKE "%' + data.right + '%" and is_del=0'
    }
    // console.log(sqlstr);
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        //保存查询数据
        let data = results
        res.send({
            data,
            ...Constant.DATA_SUCCESS
        })
    })
})

//获取商品列表
router.get('/getgoods', verify.verifyToken, (req, res) => {
    let sqlstr = 'select * from goods'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        //保存查询数据
        let data = results
        results.forEach((item, index) => {
            sqlstr = 'select * from supply where sid=' + item.supply_id
            db.query(sqlstr, (err, results) => {
                if (err) {
                    return res.send({
                        status: 403,
                        message: err.message
                    })
                }
                if (results.length == 0) {
                    // return res.send(Constant.DATA_NULL)
                    data[index].supply = []
                }
                data[index].supply = results
                //保存查询数据
                // let data2 = results
                // console.log(index,data.length-1);
                if (index == data.length - 1) {
                    return res.send({
                        data,
                        ...Constant.DATA_SUCCESS
                    })
                }

            })
        })
        // sqlstr = 'select * from supply where sid='+data.supply_id
        // db.query(sqlstr, (err, results) => {
        //     if (err) {
        //         return res.send({
        //             status: 403,
        //             message: err.message
        //         })
        //     }
        //     if (results.length == 0) {
        //         return res.send(Constant.DATA_NULL)
        //     }
        //     //保存查询数据
        //     let data2 = results
        //     res.send({
        //         data2,
        //         ...Constant.DATA_SUCCESS
        //     })
        // })
        // res.send({
        //     data,
        //     ...Constant.DATA_SUCCESS
        // })
    })
})
//添加商品
router.post('/addgoods', verify.verifyToken, (req, res) => {
    let data = req.body
    // console.log(data);
    let sqlstr = 'INSERT INTO goods (goods_name,supply_id,goods_rmb,goods_specs,remark) values("' + data.goods_name + '","' + data.supply + '","' + data.goods_rmb + '","' + data.goods_specs + '","' + data.remark + '")'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '添加失败,请稍后再试'
            })
        }
        res.send({
            status: 201,
            message: '添加成功'
        })

    })
})
//修改用户
router.post('/editgoods', verify.verifyToken, (req, res) => {
    let data = req.body
    // console.log(data);
    // if (data.powerid==undefined) {
    //     return res.send({
    //         status: 403,
    //         message: '修改失败,必须含有一个权限'
    //     })
    // }
    let sqlstr = 'UPDATE goods SET goods_name="' + data.goods_name + '",supply_id="' + data.supply + '",goods_rmb="' + data.goods_rmb + '",goods_specs="' + data.goods_specs + '",remark="' + data.remark + '"where gid="' + data.gid + '"'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '修改失败,请稍后再试'
            })
        }
        res.send({
            status: 201,
            message: '修改成功'
        })

    })
})
//删除权限
router.post('/delgoods', verify.verifyToken, (req, res) => {
    let data = req.body
    let sqlstr = 'DELETE FROM goods where gid="' + data.gid + '"'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '删除失败,请稍后再试'
            })
        }
        res.send({
            status: 201,
            message: '删除成功'
        })

    })
})
//搜索商品列表
router.post('/searchgoods', verify.verifyToken, (req, res) => {
    let data = req.body
    // console.log(data);
    let sqlstr = ''
    if (data.left != '' && data.right != '') {
        sqlstr = 'select * from goods where goods_name LIKE "%' + data.left + '%" and supply_id = "' + data.right + '"'
    } else if (data.left != '') {
        sqlstr = 'select * from goods where goods_name LIKE "%' + data.left + '%"'
    } else {
        sqlstr = 'select * from goods where supply_id = "' + data.right + '"'
    }
    // console.log(sqlstr);
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        //保存查询数据
        let data = results
        results.forEach((item, index) => {
            sqlstr = 'select * from supply where sid=' + item.supply_id
            db.query(sqlstr, (err, results) => {
                if (err) {
                    return res.send({
                        status: 403,
                        message: err.message
                    })
                }
                if (results.length == 0) {
                    // return res.send(Constant.DATA_NULL)
                    data[index].supply = []
                }
                data[index].supply = results
                //保存查询数据
                // let data2 = results
                // console.log(index,data.length-1);
                if (index == data.length - 1) {
                    return res.send({
                        data,
                        ...Constant.DATA_SUCCESS
                    })
                }

            })
        })
        // res.send({
        //     data,
        //     ...Constant.DATA_SUCCESS
        // })
    })
})

//获取仓库列表
router.get('/getstore', verify.verifyToken, (req, res) => {
    let sqlstr = 'select * from store where is_del=0'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        //保存查询数据
        let data = results
        res.send({
            data,
            ...Constant.DATA_SUCCESS
        })
    })
})
//添加仓库
router.post('/addstore', verify.verifyToken, (req, res) => {
    let data = req.body
    // console.log(data);
    let sqlstr = 'INSERT INTO store (store_name,store_address,warning_up,warning_down,is_del,lat,lng) values("' + data.store_name + '","' + data.store_address + '","' + data.warning_up + '","' + data.warning_down + '","' + data.is_del + '","' + data.lat + '","' + data.lng + '")'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '添加失败,请稍后再试'
            })
        }
        res.send({
            status: 201,
            message: '添加成功'
        })

    })
})
//修改仓库
router.post('/editstore', verify.verifyToken, (req, res) => {
    let data = req.body
    // if (data.powerid==undefined) {
    //     return res.send({
    //         status: 403,
    //         message: '修改失败,必须含有一个权限'
    //     })
    // }
    let sqlstr = 'UPDATE store SET store_name="' + data.store_name + '",store_address="' + data.store_address + '",warning_up="' + data.warning_up + '",warning_down="' + data.warning_down + '",lat="' + data.lat + '",lng="' + data.lng + '"where store_id="' + data.store_id + '"'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '修改失败,请稍后再试'
            })
        }
        res.send({
            status: 201,
            message: '修改成功'
        })

    })
})
//删除仓库
router.post('/delstore', verify.verifyToken, (req, res) => {
    let data = req.body
    // console.log(data);
    let sqlstr = 'UPDATE store SET is_del="' + 1 + '"where store_id="' + data.store_id + '"'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '删除失败,请稍后再试'
            })
        }
        res.send({
            status: 201,
            message: '删除成功'
        })

    })
})

//获取入库订单
router.get('/getput', verify.verifyToken, async (req, res) => {
    let sqlstr = 'select * from put where is_del=0'
    // let results=await db.promise().query(sqlstr)
    // let data = results
    // console.log(results[0]);
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        //保存查询数据
        let data = results
        // console.log(results);
        results.forEach(async (item, index) => {
            sqlstr = 'select * from store where is_del=0 and store_id=' + item.store_id
            // console.log(sqlstr);
            let results2 = await db.promise().query(sqlstr)
            // console.log(results2);
            data[index].store = results2[0]
            sqlstr = 'select * from goods where gid=' + item.goods_id
            let results3 = await db.promise().query(sqlstr)
            data[index].goods = results3[0]
            // console.log(results3[0][0]);
            sqlstr = 'select * from supply where sid=' + results3[0][0].supply_id
            // console.log(sqlstr);
            let results4 = await db.promise().query(sqlstr)
            data[index].supply = results4[0]
            sqlstr = 'select * from user where uid=' + item.user_id
            let results5 = await db.promise().query(sqlstr)
            data[index].user = results5[0]
            if (data.length - 1 == index) {
                res.send({
                    data,
                    ...Constant.DATA_SUCCESS
                })
            }
        })

        // console.log(data2);

    })
})
//添加入库订单
router.post('/addput', verify.verifyToken, async (req, res) => {
    let data = req.body
    // console.log(data);

    let sqlstr = 'select * from stock where store_id="' + data.store_id + '"and goods_id="' + data.goods_id + '"'
    let selectdb = await db.promise().query(sqlstr)
    // let selectstore=selectdb[0][0]
    // console.log(selectdb);
    sqlstr = 'select store.store_id as store_id,store.warning_up as up,sum(stock.number) as number from store,stock where store.store_id="' + data.store_id + '"and stock.store_id="' + data.store_id + '"'
    let sum1 = await db.promise().query(sqlstr)
    if (selectdb[0].length > 0) {
        if (sum1[0][0].up - sum1[0][0].number < data.put_sum) {
            return res.send({
                status: 403,
                message: '仓库位置不足'
            })
        }
        sqlstr = 'UPDATE stock SET number=number+"' + data.put_sum + '"where store_id="' + data.store_id + '"and goods_id="' + data.goods_id + '"'
    } else {
        if (sum1[0][0].warning_up < data.put_sum) {
            return res.send({
                status: 403,
                message: '仓库位置不足'
            })
        }
        sqlstr = 'INSERT INTO stock (store_id,goods_id,number) values("' + data.store_id + '","' + data.goods_id + '","' + data.put_sum + '")'
    }
    // let results3=await db.promise().query(sqlstr)
    // console.log(results3);

    // let sqlstr = 'INSERT INTO store (store_name,store_address,warning_up,warning_down,is_del) values("' + data.store_name + '","' + data.store_address + '","' + data.warning_up + '","' + data.warning_down + '","' + data.is_del + '")'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '添加失败,请稍后再试'
            })
        }
        data.put_data = Date.now()
        sqlstr = 'INSERT INTO put (put_data,store_id,goods_id,put_sum,put_rmb,user_id,is_del) values("' + data.put_data + '","' + data.store_id + '","' + data.goods_id + '","' + data.put_sum + '","' + data.put_rmb + '","' + data.user_id + '","' + data.is_del + '")'
        db.query(sqlstr, (err, results) => {
            if (err) {
                return res.send({
                    status: 403,
                    message: err.message
                })
            }
            if (results.affectedRows !== 1) {
                return res.send({
                    status: 403,
                    message: '添加失败,请稍后再试'
                })
            }

            res.send({
                status: 201,
                message: '添加成功'
            })

        })

    })
})
//删除入库订单
router.post('/delput', verify.verifyToken, (req, res) => {
    let data = req.body
    // console.log(data);
    let sqlstr = 'UPDATE put SET is_del="' + 1 + '"where pid="' + data.pid + '"'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '删除失败,请稍后再试'
            })
        }
        res.send({
            status: 201,
            message: '删除成功'
        })

    })
})
//搜索入库订单列表
router.post('/searchput', verify.verifyToken, (req, res) => {
    let data = req.body
    // console.log(data);
    let sqlstr = ''
    if (data.left != '' && data.right != '') {
        sqlstr = 'select * from put where pid = "' + data.left + '" and store_id = "' + data.right + '"'
    } else if (data.left != '') {
        sqlstr = 'select * from put where pid = "' + data.left + '"'
    } else if (data.right != '') {
        sqlstr = 'select * from put where store_id = "' + data.right + '"'
    }
    // console.log(sqlstr);
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        // console.log(results);
        //保存查询数据
        let data = results
        results.forEach(async (item, index) => {
            sqlstr = 'select * from store where is_del=0 and store_id=' + item.store_id
            // console.log(sqlstr);
            let results2 = await db.promise().query(sqlstr)
            // console.log(results2);
            data[index].store = results2[0]
            sqlstr = 'select * from goods where gid=' + item.goods_id
            let results3 = await db.promise().query(sqlstr)
            data[index].goods = results3[0]
            // console.log(results3[0][0]);
            sqlstr = 'select * from supply where sid=' + results3[0][0].supply_id
            // console.log(sqlstr);
            let results4 = await db.promise().query(sqlstr)
            data[index].supply = results4[0]
            sqlstr = 'select * from user where uid=' + item.user_id
            let results5 = await db.promise().query(sqlstr)
            data[index].user = results5[0]
            if (data.length - 1 == index) {
                res.send({
                    data,
                    ...Constant.DATA_SUCCESS
                })
            }
        })
        // res.send({
        //     data,
        //     ...Constant.DATA_SUCCESS
        // })
    })
})

//获取出库订单
router.get('/getout', verify.verifyToken, async (req, res) => {
    let sqlstr = 'select * from out1 where is_del=0'
    // let results=await db.promise().query(sqlstr)
    // let data = results
    // console.log(results[0]);
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        //保存查询数据
        let data = results
        // console.log(results);
        results.forEach(async (item, index) => {
            sqlstr = 'select * from store where is_del=0 and store_id=' + item.store_id
            // console.log(sqlstr);
            let results2 = await db.promise().query(sqlstr)
            // console.log(results2);
            data[index].store = results2[0]
            sqlstr = 'select * from goods where gid=' + item.goods_id
            let results3 = await db.promise().query(sqlstr)
            data[index].goods = results3[0]
            // console.log(results3[0][0]);
            sqlstr = 'select * from client where cid=' + item.client_id
            // console.log(sqlstr);
            let results4 = await db.promise().query(sqlstr)
            data[index].client = results4[0]
            sqlstr = 'select * from user where uid=' + item.user_id
            let results5 = await db.promise().query(sqlstr)
            data[index].user = results5[0]
            if (data.length - 1 == index) {
                res.send({
                    data,
                    ...Constant.DATA_SUCCESS
                })
            }
        })

        // console.log(data2);

    })
})
//添加出库订单
router.post('/addout', verify.verifyToken, async (req, res) => {
    let data = req.body
    // console.log(data);
    let sqlstr = 'select * from stock where store_id="' + data.store_id + '"and goods_id="' + data.goods_id + '"'
    let selectdb = await db.promise().query(sqlstr)
    if (selectdb[0][0].number < data.out_sum) {
        return res.send({
            status: 403,
            message: '出库失败，库存不足'
        })
    }
    if (selectdb[0].length > 0) {
        sqlstr = 'UPDATE stock SET number=number-"' + data.out_sum + '"where store_id="' + data.store_id + '"and goods_id="' + data.goods_id + '"'
    } else {
        sqlstr = 'INSERT INTO stock (store_id,goods_id,number) values("' + data.store_id + '","' + data.goods_id + '","' + data.out_sum + '")'
    }
    // let results3=await db.promise().query(sqlstr)
    // console.log(results3);

    // let sqlstr = 'INSERT INTO store (store_name,store_address,warning_up,warning_down,is_del) values("' + data.store_name + '","' + data.store_address + '","' + data.warning_up + '","' + data.warning_down + '","' + data.is_del + '")'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '添加失败,请稍后再试'
            })
        }
        data.out_data = Date.now()
        sqlstr = 'INSERT INTO out1 (out_data,store_id,goods_id,out_sum,client_id,user_id,is_del) values("' + data.out_data + '","' + data.store_id + '","' + data.goods_id + '","' + data.out_sum + '","' + data.client_id + '","' + data.user_id + '","' + data.is_del + '")'
        db.query(sqlstr, (err, results) => {
            if (err) {
                return res.send({
                    status: 403,
                    message: err.message
                })
            }
            if (results.affectedRows !== 1) {
                return res.send({
                    status: 403,
                    message: '添加失败,请稍后再试'
                })
            }

            res.send({
                status: 201,
                message: '添加成功'
            })

        })

    })
})
//搜索仓库
router.post('/searchstore', verify.verifyToken, (req, res) => {
    let data = req.body
    // console.log(data);
    let sqlstr = 'select * from stock where store_id=' + data.store
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        //保存查询数据
        // let data = results
        let data = {
            store_id: results[0].store_id
        }
        console.log(results);
        data.goods = []
        results.forEach(async (item, index) => {
            sqlstr = 'select * from goods where gid=' + item.goods_id
            let results2 = await db.promise().query(sqlstr)
            // console.log(results2[0]);
            data.goods.push(results2[0][0])
            // console.log(data);
            if (index == results.length - 1) {
                res.send({
                    data,
                    ...Constant.DATA_SUCCESS
                })
            }
        })

    })

})
//删除出库订单
router.post('/delout', verify.verifyToken, (req, res) => {
    let data = req.body
    console.log(data);
    let sqlstr = 'UPDATE out1 SET is_del="' + 1 + '"where oid="' + data.oid + '"'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '删除失败,请稍后再试'
            })
        }
        res.send({
            status: 201,
            message: '删除成功'
        })

    })
})
//搜索出库订单列表
router.post('/searchout', verify.verifyToken, (req, res) => {
    let data = req.body
    // console.log(data);
    let sqlstr = ''
    if (data.left != '' && data.right != '' && data.center != '') {
        sqlstr = 'select * from out1 where oid = "' + data.left + '" and store_id = "' + data.right + '" and client_id = "' + data.center + '"'
    } else if (data.left != '' && data.right != '') {
        sqlstr = 'select * from out1 where oid = "' + data.left + '"and store_id = "' + data.right + '"'
    } else if (data.right != '' && data.center != '') {
        sqlstr = 'select * from out1 where store_id = "' + data.right + '" and client_id = "' + data.center + '"'
    } else if (data.left != '' && data.center != '') {
        sqlstr = 'select * from out1 where oid = "' + data.left + '" and client_id = "' + data.center + '"'
    } else if (data.left != '') {
        sqlstr = 'select * from out1 where oid = "' + data.left + '"'
    } else if (data.right != '') {
        sqlstr = 'select * from out1 where store_id = "' + data.right + '"'
    } else if (data.center != '') {
        sqlstr = 'select * from out1 where client_id = "' + data.center + '"'
    }
    // console.log(sqlstr);
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        // console.log(results);
        //保存查询数据
        let data = results
        results.forEach(async (item, index) => {
            sqlstr = 'select * from store where is_del=0 and store_id=' + item.store_id
            // console.log(sqlstr);
            let results2 = await db.promise().query(sqlstr)
            // console.log(results2);
            data[index].store = results2[0]
            sqlstr = 'select * from goods where gid=' + item.goods_id
            let results3 = await db.promise().query(sqlstr)
            data[index].goods = results3[0]
            // console.log(results3[0][0]);
            sqlstr = 'select * from client where cid=' + item.client_id
            // console.log(sqlstr);
            let results4 = await db.promise().query(sqlstr)
            data[index].client = results4[0]
            sqlstr = 'select * from user where uid=' + item.user_id
            let results5 = await db.promise().query(sqlstr)
            data[index].user = results5[0]
            if (data.length - 1 == index) {
                res.send({
                    data,
                    ...Constant.DATA_SUCCESS
                })
            }
        })
        // res.send({
        //     data,
        //     ...Constant.DATA_SUCCESS
        // })
    })
})

//获取仓库详情
router.get('/getstock', verify.verifyToken, async (req, res) => {
    let sqlstr = 'select * from stock'
    // let results=await db.promise().query(sqlstr)
    // let data = results
    // console.log(results[0]);
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        //保存查询数据
        let data = results
        // console.log(results);
        results.forEach(async (item, index) => {
            sqlstr = 'select * from store where is_del=0 and store_id=' + item.store_id
            // console.log(sqlstr);
            let results2 = await db.promise().query(sqlstr)
            // console.log(results2);
            data[index].store = results2[0]
            sqlstr = 'select * from goods where gid=' + item.goods_id
            let results3 = await db.promise().query(sqlstr)
            data[index].goods = results3[0]
            // console.log(results3[0][0]);
            if (data.length - 1 == index) {
                res.send({
                    data,
                    ...Constant.DATA_SUCCESS
                })
            }
        })

        // console.log(data2);

    })
})
//修改用户
router.post('/editstock', verify.verifyToken, (req, res) => {
    let data = req.body
    console.log(data);
    // if (data.powerid==undefined) {
    //     return res.send({
    //         status: 403,
    //         message: '修改失败,必须含有一个权限'
    //     })
    // }
    let sqlstr = 'UPDATE stock SET stock_up="' + data.stock_up + '",stock_down="' + data.stock_down + '"where select_id="' + data.select_id + '"'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 403,
                message: '修改失败,请稍后再试'
            })
        }
        res.send({
            status: 201,
            message: '修改成功'
        })

    })
})
//搜索出库订单列表
router.post('/searchstock', verify.verifyToken, (req, res) => {
    let data = req.body
    // console.log(data);
    let sqlstr = ''
    if (data.left != '' && data.right != '') {
        sqlstr = 'select * from stock where goods_id = "' + data.right + '" and store_id = "' + data.left + '"'
    } else if (data.left != '') {
        sqlstr = 'select * from stock where goods_id = "' + data.left + '"'
    } else if (data.right != '') {
        sqlstr = 'select * from stock where store_id = "' + data.right + '"'
    }
    // console.log(sqlstr);
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        // console.log(results);
        //保存查询数据
        let data = results
        results.forEach(async (item, index) => {
            sqlstr = 'select * from store where is_del=0 and store_id=' + item.store_id
            // console.log(sqlstr);
            let results2 = await db.promise().query(sqlstr)
            // console.log(results2);
            data[index].store = results2[0]
            sqlstr = 'select * from goods where gid=' + item.goods_id
            let results3 = await db.promise().query(sqlstr)
            data[index].goods = results3[0]
            // console.log(results3[0][0]);
            if (data.length - 1 == index) {
                res.send({
                    data,
                    ...Constant.DATA_SUCCESS
                })
            }
        })
        // res.send({
        //     data,
        //     ...Constant.DATA_SUCCESS
        // })
    })
})

//仓库警报
router.get('/stocksum', verify.verifyToken, (req, res) => {
    let data = req.body
    // console.log(data);
    let sqlstr = 'select * from store where is_del=0'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        //保存查询数据
        let data = results
        // let data={store_id:results[0].store_id}
        results.forEach(async (item, index) => {
            sqlstr = 'select sum(number) as sum from stock where store_id="' + item.store_id + '"'
            let results2 = await db.promise().query(sqlstr)
            data[index].sum = results2[0][0].sum
            // console.log(data[index]);
            if (data[index].warning_down - 0 > results2[0][0].sum - 0) {
                data[index].rank = 0
            } else if (data[index].warning_down - 0 <= results2[0][0].sum - 0 && data[index].warning_up - 0 > results2[0][0].sum - 0) {
                data[index].rank = 1
            } else {
                data[index].rank = 2
            }
            // console.log(data);
            if (index == results.length - 1) {
                res.send({
                    data,
                    ...Constant.DATA_SUCCESS
                })
            }
        })


    })

})
//商品警报
router.post('/goodssum', verify.verifyToken, (req, res) => {
    let data_up = req.body
    // console.log(data);
    let sqlstr = 'select *,number as sum from stock where store_id="'+data_up.store_id+'"'
    db.query(sqlstr, (err, results) => {
        if (err) {
            return res.send({
                status: 403,
                message: err.message
            })
        }
        if (results.length == 0) {
            return res.send(Constant.DATA_NULL)
        }
        //保存查询数据
        let data = results
        // let data={store_id:results[0].store_id}
        results.forEach(async (item, index) => {
            sqlstr = 'select * from goods where gid="' + item.goods_id + '"'
            let results2 = await db.promise().query(sqlstr)
            data[index].goods = results2[0][0]
            // console.log(data[index]);
            if (item.sum - 0 < item.stock_down - 0) {
                data[index].rank = 0
            } else if (item.stock_down - 0 <= item.sum - 0 && item.stock_up - 0 > item.sum - 0) {
                data[index].rank = 1
            } else if(item.stock_up==null&&item.stock_down==null){
                data[index].rank = 3
            }else if(item.sum - 0 >= item.stock_up - 0){
                data[index].rank = 2
            }
            // console.log(data);
            if (index == results.length - 1) {
                res.send({
                    data,
                    ...Constant.DATA_SUCCESS
                })
            }
        })


    })

})
module.exports = router